// pages/modifyphone/modifyphone.js
var util = require('../../utils/util.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    reg_hqyzm: "获取验证码",
    countdown_time: 60,
    showtime1:true,
    phone:'',
    code:''
  },
  inputPhone:function(e){
    let value = e.detail.value;
    this.setData({
      phone:value
    })
  },
  isPoneAvailable: function(poneInput) {
    var phoneReg = /^1[3-578]\d{9}$/;
    if (phoneReg.test(poneInput)) {
      return true;
    } else {
      return false;
    }
  },
  reg_yanzhengma_huoqu: function () {
    var that = this;
    let phone = that.data.phone;
    let flag = that.isPoneAvailable(phone);
    if (!flag) {
      wx.showToast({
        title: '手机格式不正确',
        icon: 'none'
      })
      return;
    }
    util.commonRequest('verification', 'post', { phone: phone }, function (res) {
      if (res.data.code == 200) {
        that.setData({
          showtime1: false,
          showtime2: true,
        });
        countdown(that);
        wx.showToast({
          title: '发送成功',
        })

      } else {
        wx.showToast({
          icon: 'none',
          title: res.data.msg,
        })
      }
    });


  },
  inputCode:function(e){
    let code = e.detail.value;
    this.setData({
      code:code
    })
  },
  submit:function(){
    var that = this;
    let phone = that.data.phone;
    let code = that.data.code;
    util.commonRequest('verPhone', 'post', { phone: phone, code: code}, function (res) {
      if (res.data.code == 200) {
        wx.showToast({
          title: res.data.msg,
        })
        app.globalData.modifyPhone = phone;
      
        wx.navigateBack({
          data:1
        })

      } else {
        wx.showToast({
          icon: 'none',
          title: res.data.msg,
        })
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
function countdown(that) {
  var second = that.data.countdown_time
  if (second == 0) {
    console.log("001");
    that.setData({
      showtime1: true,
      showtime2: false,
      reg_hqyzm: "重新获取",
      countdown_time: 60,
    });
    console.log("002");
    return;
  }
  console.log("003");
  var time = setTimeout(function () {
    that.setData({
      countdown_time: second - 1
    });
    countdown(that);
  }
    , 1000)
}